﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EpiPoker.BusinessManagementObjects
{
    /// <summary>
    /// Represent all enabled actions for the deck
    /// </summary>
    public class DeckActions
    {
        /// <summary>
        /// Mix the deck
        /// </summary>
        /// <param name="deck">The deck</param>
        /// <returns>The deck mixed</returns>
        public static Objects.Deck mixer(Objects.Deck deck)
        {
            Random random = new Random();
            List<Objects.Card> cartes = deck.Cards;

            for (int i = 0; i < cartes.Count; i++)
            {
                int nb = random.Next(0, cartes.Count);
                Objects.Card tmp = cartes[nb];
                cartes[nb] = cartes[i];
                cartes[i] = tmp;
            }

            deck.Cards = cartes;

            return deck;
        }

        /// <summary>
        /// Distribute two cards for each players in game
        /// </summary>
        /// <param name="list_users">The list of users in game</param>
        /// <param name="deck">The deck mixed</param>
        /// <returns>A list of Hands, one per players</returns>
        public static List<Objects.Hand> distribute(List<DataAccess.T_User> list_users, Objects.Deck deck)
        {
            List<Objects.Hand> res = new List<Objects.Hand>();


            for (int i = 0; i < list_users.Count; i++)
            {
                Objects.Hand hand = new Objects.Hand();
                hand.First = deck.Cards.First();

                hand.User = list_users[i];
                hand.IsPlaying = false;

                deck.Cards.RemoveAt(0);
                res.Add(hand);
            }


            for (int i = 0; i < list_users.Count; i++)
            {
                res[i].Second = deck.Cards.First();
                deck.Cards.RemoveAt(0);
            }

            return res;
        }

        /// <summary>
        /// Put in a list the three card in the flop
        /// </summary>     
        /// <param name="deck">The deck mixed and distributed</param>
        /// <returns>A list of Three cards for the flop</returns>
        public static List<Objects.Card> flop(Objects.Deck deck)
        {
            List<Objects.Card> res = new List<Objects.Card>();

            deck.Cards.RemoveAt(0);
            for (int i = 0; i < 3; i++)
            {
                res.Add(deck.Cards.First());
                deck.Cards.RemoveAt(0);
            }

            return res;
        }

        /// <summary>
        /// The card for the turn
        /// </summary>     
        /// <param name="deck">The deck mixed and distributed and flopped</param>
        /// <returns>The card for the turn</returns>
        public static Objects.Card turn(Objects.Deck deck)
        {
            deck.Cards.RemoveAt(0);
            Objects.Card card = deck.Cards.First();
            deck.Cards.RemoveAt(0);

            return card;
        }

        /// <summary>
        /// The card for the river
        /// </summary>     
        /// <param name="deck">The deck mixed and distributed, flopped and turned</param>
        /// <returns>The card for the river</returns>
        public static Objects.Card river(Objects.Deck deck)
        {
            deck.Cards.RemoveAt(0);
            Objects.Card card = deck.Cards.First();
            deck.Cards.RemoveAt(0);

            return card;
        }
    }
}